var roleCarrierdrop = {

    /** @param {Creep} creep **/
    run: function(creep) {
        //creep.say(creep.name);
        //creep.say(creep.memory.building);
        if(creep.store[RESOURCE_ENERGY] == 0){
            creep.memory.building = true;
        }
        else{
            creep.memory.building = false;
        }

        if(creep.memory.building == true){
            //creep.say(creep.memory.building);
            // var targetsgo = creep.room.find(FIND_TOMBSTONES, {
            //     filter: (str) => {
            //         return str.store.getUsedCapacity(RESOURCE_ENERGY)>100;//大于100的墓碑
            //     }
            // });
            //var targetsres = creep.room.find(FIND_DROPPED_RESOURCES);
            //console.log('墓碑',targetsgo[0].store.getUsedCapacity());
            var targetsterminal = creep.room.find(FIND_STRUCTURES, {
                filter: (structure) => {
                    return (structure.structureType == STRUCTURE_TERMINAL) && 
                    structure.store.getUsedCapacity(RESOURCE_ENERGY) > 60000;//dayu60000-
                }
            });
            // if(targetsgo.length > 0 && targetsgo[0].store.getUsedCapacity()>100) {
            //     creep.say('有墓碑');
            //     if(creep.withdraw(targetsgo[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
            //         creep.moveTo(targetsgo[0],{visualizePathStyle: {stroke: '#00ff00'}});
            //     }
            // }
            // if(targetsres.length>0 && targetsres[0].amount>100){
            //     creep.say('有掉落');
            //     if(creep.pickup(targetsres[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
            //     creep.moveTo(targetsres[0],{visualizePathStyle: {stroke: '#00ff00'}});
            //     }
            // }
            if(targetsterminal.length>0){
                creep.say('有交易');
                if(creep.withdraw(targetsterminal[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                creep.moveTo(targetsterminal[0],{visualizePathStyle: {stroke: '#00ff00'}});
                }
            }
            
            else{
                //container暂时没了
                // var targetshar = creep.room.find(FIND_STRUCTURES, {
                //     filter: (structure) => {
                //         return structure.structureType == STRUCTURE_CONTAINER && structure.store.getUsedCapacity() > 0;
                //     }
                // });
                // if(targetshar.length > 0) {
                //     if(creep.withdraw(targetshar[1], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                //         creep.moveTo(targetshar[1]);
                //         //creep.say('搬来');
                //     }
                // }

                //兼任link搬运
                var targetslink = Game.rooms['E9S58'].lookForAt('structure',29, 22)[0];
                //console.log(targetslink);
                if(targetslink) {
                    creep.say('有link');
                    if(creep.withdraw(targetslink, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                        creep.moveTo(targetslink,{visualizePathStyle: {stroke: '#00ff00'}});
                        //creep.say('搬来');
                    }
                }   
            }   
        }
        else{
            var targets = creep.room.find(FIND_STRUCTURES, {
                filter: (structure) => {
                    return (structure.structureType == STRUCTURE_STORAGE) && 
                    structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
                }
            });
            if(targets.length > 0) {
                if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(targets[0],{visualizePathStyle: {stroke: '#00ff00'}});
                    //creep.say('搬去');
                }
            }
        }
	}
};

module.exports = roleCarrierdrop;